package com.md.api.controller;

import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.PhotoInfo;
import com.md.api.entity.PictureInfo;
import com.md.api.entity.ShareRecord;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.PhotoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/photo")
@RestController
public class PhotoController {




	@Resource
    PhotoService photoSevice;



	@RequestMapping("/photoPage")
	public HttpResult page(HttpServletRequest request, @RequestBody ComposeQueryDto qto) {
		return photoSevice.photoPage(request,qto);
	}

	@RequestMapping("/picturePage")
	public HttpResult picturePage(HttpServletRequest request, @RequestBody ComposeQueryDto qto) {
		return photoSevice.picturePage(request,qto);
	}

	@RequestMapping("/newOne")
	public HttpResult newOne(HttpServletRequest request, @RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getPhotoName())){
			return HttpResult.fail("相冊名稱不能爲空！");
		}
		if (null==photoInfo.getValidDate()){
			return HttpResult.fail("相冊活動時間不能爲空！");
		}
		return photoSevice.newOne(request,photoInfo);

	}

	@RequestMapping("/savePicture")
	public HttpResult savePicture(HttpServletRequest request, @RequestBody PictureInfo pictureInfo) {
		if (StringUtils.isEmpty(pictureInfo.getPhotoId())
				||StringUtils.isEmpty(pictureInfo.getPictureUrl())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.savePicture(request,pictureInfo);

	}

	@RequestMapping("/deletePicture")
	public HttpResult deletePicture(HttpServletRequest request,@RequestBody PictureInfo picture) {
		return photoSevice.deletePicture(request,picture);
	}

	//转移
	@RequestMapping("/transferPicture")
	public HttpResult transferPicture(HttpServletRequest request, @RequestBody ComposeCommitDto commitDto) {
		if (CollectionUtils.isEmpty(commitDto.getPictureIds())
				||StringUtils.isEmpty(commitDto.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.transferPicture(request,commitDto);

	}



	@PostMapping("/refinePicture")
	public HttpResult refinePicture(HttpServletRequest request, @RequestBody PictureInfo picture) {
		if (StringUtils.isEmpty(picture.getPictureId())
				||StringUtils.isEmpty(picture.getPictureUrl())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.refinePicture(request,picture);
	}

	@RequestMapping("/setPhotoVideoUrl")
	public HttpResult setPhotoVideoUrl(HttpServletRequest request, @RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getVideoUrls())||StringUtils.isEmpty(photoInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.setPhotoVideoUrl(request,photoInfo);
	}

	@RequestMapping("/setPhotoLogoUrl")
	public HttpResult setPhotoLogoUrl(HttpServletRequest request, @RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getLogoUrl())||StringUtils.isEmpty(photoInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.setPhotoLogoUrl(request,photoInfo);
	}

	//设为私密
	@RequestMapping("/setPrivate")
	public HttpResult setPrivate(HttpServletRequest request, @RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.setPrivate(request,photoInfo);
	}

	@RequestMapping("/resetPhotoName")
	public HttpResult resetPhotoName(HttpServletRequest request, @RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getPhotoName())||StringUtils.isEmpty(photoInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.resetPhotoName(request,photoInfo);
	}

	@RequestMapping("/resetPictureName")
	public HttpResult resetPictureName(HttpServletRequest request, @RequestBody PictureInfo pictureInfo) {
		if (StringUtils.isEmpty(pictureInfo.getPictureName())||StringUtils.isEmpty(pictureInfo.getPictureId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.resetPictureName(request,pictureInfo);
	}

	//设为封面
	@RequestMapping("/setCoverImage")
	public HttpResult setCoverImage(HttpServletRequest request, @RequestBody PictureInfo pictureInfo) {
		if (StringUtils.isEmpty(pictureInfo.getPictureId())||StringUtils.isEmpty(pictureInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.setCoverImage(request,pictureInfo);
	}

	//设为精选
	@RequestMapping("/setCherryPick")
	public HttpResult setCherryPick(HttpServletRequest request, @RequestBody ComposeCommitDto commitDto) {
		if (CollectionUtils.isEmpty(commitDto.getPictureIds())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.setCherryPick(request,commitDto);
	}

	//取消封面
	@RequestMapping("/removeCoverImage")
	public HttpResult removeCoverImage(HttpServletRequest request, @RequestBody PictureInfo pictureInfo) {
		if (StringUtils.isEmpty(pictureInfo.getPictureId())||StringUtils.isEmpty(pictureInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.removeCoverImage(request,pictureInfo);
	}

	//取消精选
	@RequestMapping("/removeCherryPick")
	public HttpResult removeCherryPick(HttpServletRequest request, @RequestBody PictureInfo pictureInfo) {
		if (StringUtils.isEmpty(pictureInfo.getPictureId())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.removeCherryPick(request,pictureInfo);
	}

	//创建分享记录  访问密码
	@RequestMapping("/saveShare")
	public HttpResult saveShare(HttpServletRequest request, @RequestBody ShareRecord shareRecord) {
		if (null==shareRecord.getShareType()||StringUtils.isEmpty(shareRecord.getSecret())){
			return HttpResult.PARAM_ERROR;
		}
		return photoSevice.saveShare(request,shareRecord);
	}

}
